import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/http/api/lot_api.dart';

import '../../bean/arrtibut_balue_entity.dart';
import 'TestLotStartWorkState.dart';

class TestLotStartWorkController extends BaseController {
  final TestLotStartWorkState state = TestLotStartWorkState();

  getLotInfo(String lotID) async {
    Map<String, dynamic> params = {'LOTID': lotID};
    var result = await LotApi.lotInfo(canelToken, params: params);
    if (result.ok) {
      state.lotEntity = result.data;
      update([state.lotInfoID, state.materialInfoID]);
    }
  }

  trackIn() async {
    if (state.controller1.text.isEmpty) {
      Get.snackbar("提示", "请扫描批次号");
      return;
    }
    if (state.controller2.text.isEmpty) {
      Get.snackbar("提示", "请扫描设备信息");
      return;
    }

    if (state.controller5.text.isEmpty) {
      Get.snackbar("提示", "请输入半成品物料投入数");
      return;
    }
    List<String> list = [];
    List<LotEntity> lotList = [];
    list.add(state.controller3.text);
    list.add(state.controller4.text);
    lotList.add(state.lotEntity ?? LotEntity());

    List<ArrtibutBalueEntity> arrList = [];
    ArrtibutBalueEntity arr = ArrtibutBalueEntity();
    arr.aTTRIBUTENAME = 'inputNumber';
    arr.aTTRIBUTEVALUE = state.controller5.text;
    arrList.add(arr);
    Map<String, dynamic> params = {
      'ACTIONTYPE': 'FTTrackIn',
      'EQUIPMENTID': state.controller2.text,
      'LOTLIST': lotList,
      'SUBEQUIPMENTIDLIST': list,
      'MAINMLOTLIST' : state.lotEntity?.MAINBOMLINELIST ?? [],
      'STEPMLOTLIST' : state.lotEntity?.bOMLINELIST ?? [],
      'LOTATTRIBUTEVALUELIST' : arrList
    };
    var result = await LotApi.testTrackIn(canelToken, params: params);
    if (result.ok) {
      Fluttertoast.showToast(msg: "进站成功");
      Get.back();
    }
  }

  void addTempMaterial() {
    if ((state.lotEntity?.MAINBOMLINELIST ?? []).isNotEmpty) {
      var lotBOMLINELIST = (state.lotEntity?.MAINBOMLINELIST ?? [])[0];
      lotBOMLINELIST.MLOTID = '';
      (state.lotEntity?.MAINBOMLINELIST ?? []).add(lotBOMLINELIST);
      update([state.materialInfoID]);
    }
  }

  void removeTempMaterial() {
    if ((state.lotEntity?.MAINBOMLINELIST ?? []).isNotEmpty) {
      if ((state.lotEntity?.MAINBOMLINELIST ?? []).length == 1) {
        return;
      }
      (state.lotEntity?.MAINBOMLINELIST ?? []).removeAt((state.lotEntity?.MAINBOMLINELIST ?? []).length - 1);
      update([state.materialInfoID]);
    }
  }
}
